﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.IsolatedStorage;
using System.Windows;
using Microsoft.Xna.Framework;

namespace Animator
{
    public class CharDef
    {
        Animation[] animations;
        Frame[] frames;

        public string Path;

        public int HeadIndex, TorsoIndex, LegsIndex, WeaponIndex;

        public Animation[] Animations { get { return animations; } }
        public Frame[] Frames { get { return frames; } }

        public CharDef()
        {
            animations = new Animation[64];
            for (int i = 0; i < animations.Length; i++)
            {
                animations[i] = new Animation();
            }

            frames = new Frame[512];
            for (int i = 0; i < frames.Length; i++)
            {
                frames[i] = new Frame();
            }

            Path = "char";
        }
        public CharDef(String path)
        {
            Path = path;
            animations = new Animation[64];
            for (int i = 0; i < animations.Length; i++)
            {
                animations[i] = new Animation();
            }

            frames = new Frame[512];
            for (int i = 0; i < frames.Length; i++)
            {
                frames[i] = new Frame();
            }
            Read();
        }

        public void Write()
        {
            BinaryWriter b = new BinaryWriter(File.Open(@"data/" + Path + ".zmx", FileMode.Create));
            

            b.Write(Path);
            b.Write(HeadIndex);
            b.Write(TorsoIndex);
            b.Write(LegsIndex);
            b.Write(WeaponIndex);

            for (int i = 0; i < animations.Length; i++)
            {
                b.Write(animations[i].name);

                for (int j = 0; j < animations[i].KeyFrames.Length; j++)
                {
                    KeyFrame keyFrame = animations[i].KeyFrames[j];
                    b.Write(keyFrame.FrameRef);
                    b.Write(keyFrame.Duration);
                    String[] scripts = keyFrame.Scripts;
                    for (int s = 0; s < scripts.Length; s++)
                    {
                        b.Write(scripts[s]);
                    }
                }
            }

            for (int i = 0; i < frames.Length; i++)
            {
                b.Write(frames[i].name);

                for (int j = 0; j < frames[i].Parts.Length; j++)
                {
                    Part p = frames[i].Parts[j];
                    b.Write(p.Index);
                    b.Write(p.Location.X);
                    b.Write(p.Location.Y);
                    b.Write(p.Rotation);
                    b.Write(p.Scaling.X);
                    b.Write(p.Scaling.Y);
                    b.Write(p.Flip);
                }
            }
            b.Close();
            Console.WriteLine("Saved.");
        }

        public void Read()
        {
            //BinaryReader b = new BinaryReader(File.Open(@"data/" + Path + ".zmx", FileMode.Open, FileAccess.Read));

            //IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
            //string temp = Path;
            //IsolatedStorageFileStream stream = new IsolatedStorageFileStream( Path + ".zmx",
            //     FileMode.OpenOrCreate, store);
            ////IsolatedStorageFileStream stream = new IsolatedStorageFileStream(@"data/" + Path + ".zmx", FileMode.Open, FileAccess.Read);
            ////StreamReader b = new StreamReader(stream);
            //BinaryReader b = new BinaryReader(stream);
            //var resource = GetResourceStream(new Uri("textfile.txt", UriKind.Relative);
            //App.GetResourceStrema(new Uri
            //Uri uri = new Uri("\\data\\red.zmx", UriKind.Relative);
            BinaryReader b = new BinaryReader(TitleContainer.OpenStream(Path));

            //using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            //{
            //    string[] files = store.GetDirectoryNames("*");
            //    string[] files2 = store.GetFileNames();
            //    using (var isfs = new IsolatedStorageFileStream("red1.zmx", FileMode.Open, store))
            //    {
            //        using (var sw = new StreamReader(isfs))
            //        {
            //            //sw.Write(serializedCollectionObject);
            //            sw.Close();
            //        }
            //    }
            //}



            Path = b.ReadString();
            HeadIndex = b.ReadInt32();
            TorsoIndex = b.ReadInt32();
            LegsIndex = b.ReadInt32();
            WeaponIndex = b.ReadInt32();

            for (int i = 0; i < animations.Length; i++)
            {
                animations[i].name = b.ReadString();

                for (int j = 0; j < animations[i].KeyFrames.Length; j++)
                {
                    KeyFrame keyFrame = animations[i].KeyFrames[j];
                    keyFrame.FrameRef = b.ReadInt32();
                    keyFrame.Duration = b.ReadInt32();

                    string[] scripts = keyFrame.Scripts;

                    for (int s = 0; s < scripts.Length; s++)
                    {
                        scripts[s] = b.ReadString();
                    }
                }
            }

            for (int i = 0; i < frames.Length; i++)
            {
                frames[i].name = b.ReadString();

                for (int j = 0; j < frames[i].Parts.Length; j++)
                {
                    Part p = frames[i].Parts[j];

                    p.Index = b.ReadInt32();
                    p.Location.X = b.ReadSingle();
                    p.Location.Y = b.ReadSingle();
                    p.Rotation = b.ReadSingle();
                    p.Scaling.X = b.ReadSingle();
                    p.Scaling.Y = b.ReadSingle();
                    p.Flip = b.ReadInt32();
                }
            }

            b.Close();

            Console.WriteLine("Loaded");
        }
    }
}
